package encrpc

import (
	context "context"

	"gitee.com/fhe-search/fhe-search-rpc/encsdk"
)

type EncServer struct {
	UnimplementedEncServiceServer
}

func (s *EncServer) SymEncUtf8(ctx context.Context, req *SymEncUtf8Request) (*SymEncUtf8Response, error) {
	enc, err := encsdk.SymEncUtf8(req.Utf8ClearText)
	if err != nil {
		return &SymEncUtf8Response{ErrorMessage: err.Error()}, err
	} else {
		return &SymEncUtf8Response{EncryptedData: enc}, nil
	}
}

func (s *EncServer) SymDecUtf8(ctx context.Context, req *SymDecUtf8Request) (*SymDecUtf8Response, error) {
	dec, err := encsdk.SymDecUtf8(req.Utf8EncText)
	if err != nil {
		return &SymDecUtf8Response{ErrorMessage: err.Error()}, err
	} else {
		return &SymDecUtf8Response{DecryptedData: dec}, nil
	}
}

func (s *EncServer) SymEncS32Ex(ctx context.Context, req *SymEncS32ExRequest) (*SymEncS32ExResponse, error) {
	enc, err := encsdk.SymEncS32Ex(req.Int32Arr)
	if err != nil {
		return &SymEncS32ExResponse{ErrorMessage: err.Error()}, err
	} else {
		return &SymEncS32ExResponse{EncryptedData: enc}, nil
	}
}

func (s *EncServer) SymEncS32(ctx context.Context, req *SymEncS32Request) (*SymEncS32Response, error) {
	enc, err := encsdk.SymEncS32(req.Plain)
	if err != nil {
		return &SymEncS32Response{ErrorMessage: err.Error()}, err
	} else {
		return &SymEncS32Response{EncryptedData: enc}, nil
	}
}

func (s *EncServer) SymDecS32(ctx context.Context, req *SymDecS32Request) (*SymDecS32Response, error) {
	dec, err := encsdk.SymDecS32(req.EncData)
	if err != nil {
		return &SymDecS32Response{ErrorMessage: err.Error()}, err
	} else {
		return &SymDecS32Response{DecryptedData: dec}, nil
	}
}

func (s *EncServer) FheEncUtf8(ctx context.Context, req *FheEncUtf8Request) (*FheEncUtf8Response, error) {
	enc, err := encsdk.FheEncUtf8(req.Utf8Text)
	if err != nil {
		return &FheEncUtf8Response{ErrorMessage: err.Error()}, err
	} else {
		return &FheEncUtf8Response{EncryptedData: enc}, nil
	}
}

func (s *EncServer) FheDecUtf8(ctx context.Context, req *FheDecUtf8Request) (*FheDecUtf8Response, error) {
	dec, err := encsdk.FheDecUtf8(req.Utf8EncText)
	if err != nil {
		return &FheDecUtf8Response{ErrorMessage: err.Error()}, err
	} else {
		return &FheDecUtf8Response{DecryptedData: dec}, nil
	}
}

func (s *EncServer) FheSub(ctx context.Context, req *FheSubRequest) (*FheSubResponse, error) {
	diff, err := encsdk.FheSub(req.Enc1, req.Enc2)
	if err != nil {
		return &FheSubResponse{ErrorMessage: err.Error()}, err
	} else {
		return &FheSubResponse{Diff: diff}, nil
	}
}

func (s *EncServer) FheDecDiff(ctx context.Context, req *FheDecDiffRequest) (*FheDecDiffResponse, error) {
	dec, err := encsdk.FheDecDiff(req.Diff)
	if err != nil {
		return &FheDecDiffResponse{ErrorMessage: err.Error()}, err
	} else {
		return &FheDecDiffResponse{Result: dec}, nil
	}
}

// GetBucket8 获取term的bucket8
func (s *EncServer) GetBucket8(ctx context.Context, req *GetBucket8Request) (resp *GetBucket8Response, err error) {
	defer func() {
		if err := recover(); err != nil {
			resp = &GetBucket8Response{Bucket8: 0}
			err = err.(error)
		}
	}()
	bucket8 := encsdk.GetBucket8(req.Term)
	return &GetBucket8Response{Bucket8: int32(bucket8)}, nil
}